home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n14.arc / TRYVECS.CPP < prev    next >
C/C++ Source or Header  |  1991-07-11  |  3KB  |  97 lines

  1. // TRYVECS.CPP - Try Overloaded + Operator for Vector Data Type
  2. // Compile with Borland C++ 2.0
  3. // Copyright (C) 1991 Ziff Davis Communications
  4. // PC Magazine * Ray Duncan April 1991
  5.  
  6. // Note: all directions for input and output are in degrees,
  7. // but calculations are carried out internally in radians.
  8.  
  9. #include <math.h>
  10. #include <iostream.h>
  11.  
  12. const double pi = 3.141592654;              // constant Pi
  13.  
  14.                                             // local function prototypes
  15. double deg2rad(double);                     // convert degrees to radians
  16. double rad2deg(double);                     // convert radians to degrees
  17.  
  18. struct VECTOR {                             // vector data type
  19.     double magnitude;
  20.     double direction; } ;
  21.  
  22. VECTOR operator + (VECTOR A, VECTOR B);     // vector operator prototype
  23.  
  24. main()
  25. {
  26.     VECTOR A, B, C;                         // instantiate 3 vectors
  27.  
  28.     cout << "\nAdd two vectors.";
  29.     cout << "\nNote: directions are entered in degrees!\n\n";
  30.  
  31.     cout << "Enter Vector A magnitude: ";   // prompt for Vector A
  32.     cin  >> A.magnitude;
  33.     cout << "Enter Vector A direction: ";
  34.     cin  >> A.direction;
  35.  
  36.     cout << "Enter Vector B magnitude: ";   // prompt for Vector B
  37.     cin  >> B.magnitude;
  38.     cout << "Enter Vector B direction: ";
  39.     cin  >> B.direction;
  40.  
  41.     A.direction = deg2rad(A.direction);     // convert degrees to radians
  42.     B.direction = deg2rad(B.direction);
  43.  
  44.     C = A + B;                              // add the vectors
  45.  
  46.     C.direction = rad2deg(C.direction);     // radians to degrees
  47.  
  48.     cout << "\nVector result: "             // display the result
  49.          << "  magnitude = " << C.magnitude
  50.          << "  direction = " << C.direction << "\n" ;
  51. }
  52.  
  53.  
  54. VECTOR operator + (VECTOR A, VECTOR B)      // vector addition operator
  55. {
  56.     VECTOR temp;                            // scratch storage
  57.     double angle;                           // scratch storage
  58.  
  59.     angle = B.direction - A.direction;      // find angle between vectors
  60.  
  61.     if (angle == pi)                        // special handling to avoid
  62.     {                                       // overflow for angle=180
  63.         temp.magnitude = fabs(A.magnitude - B.magnitude);
  64.         temp.direction = A.magnitude>B.magnitude ? A.direction : B.direction;
  65.         return temp;
  66.     }
  67.  
  68.     if ((A.magnitude == 0) && (B.magnitude == 0))
  69.     {
  70.         temp.magnitude = 0;                 // special handling to avoid
  71.         temp.direction = 0;                 // divide by zero if both
  72.         return temp;                        // magnitudes = 0
  73.     }
  74.  
  75.     temp.magnitude = sqrt(                  // find magnitude of result
  76.                      (A.magnitude * A.magnitude) +
  77.                      (B.magnitude * B.magnitude) +
  78.                      (2 * A.magnitude * B.magnitude * cos(angle)));
  79.  
  80.     temp.direction = A.direction +          // find direction of result
  81.                      asin(((B.magnitude * sin(angle))/temp.magnitude));
  82.  
  83.     return temp;                            // return resultant vector
  84. }
  85.  
  86.  
  87. double deg2rad(double degrees)              // convert degrees to radians
  88. {
  89.     return ((degrees * 2 * pi)/360);
  90. }
  91.  
  92.  
  93. double rad2deg(double radians)              // convert radians to degrees
  94. {
  95.     return ((radians * 360)/(2 * pi));
  96. }
  97.